package org.groupg.demo.sshd;

import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.shell.InteractiveProcessShellFactory;
import org.apache.sshd.server.auth.password.PasswordAuthenticator;
import org.apache.sshd.server.session.ServerSession;

public class SshServerExample {
    public static void main(String[] args) throws Exception {
        SshServer sshd = SshServer.setUpDefaultServer();
        sshd.setPort(2222);

        // 设置主机密钥
        sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());

        // 设置密码认证
        sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
            @Override
            public boolean authenticate(String username, String password, ServerSession session) {
                return "admin".equals(username) && "password".equals(password);
            }
        });

        // 设置shell
        sshd.setShellFactory(new InteractiveProcessShellFactory());

        sshd.start();
        System.out.println("SSH server started on port 2222");

        // 保持服务器运行
        Thread.sleep(Long.MAX_VALUE);
    }
}